/**
 * Created by Administrator on 2016/7/15.
 */
(function () {
	'use strict';

	angular
		.module('client')
		.controller('LogcatController', LogcatController);

	LogcatController.$inject = ['$stateParams', '$state', '$log', 'logcatService'];
	function LogcatController($stateParams, $state, $log, logcatService) {
		var logCtrl = this;
		logCtrl.logcatState = false;
		logCtrl.logs = [];
		logCtrl.level = '2';
		logCtrl.search = {};
		logCtrl.isMac = logcatService.isMac();

		logCtrl.updateLogcatState = updateLogcatState;
		logCtrl.clearLogcatState = logcatService.clearLogcatState;
		logCtrl.changeLevel = changeLevel;
		logCtrl.exportLogcatData = logcatService.exportLogcatData;

		function updateLogcatState() {
			var data;
			logCtrl.logcatState = !logCtrl.logcatState;
			if (logCtrl.logcatState) {
				logcatService.openWs( $stateParams.deviceID, logCtrl.logs, logCtrl.level);
			}
			else {
				logcatService.closeWs();
				if(logCtrl.isMac) {
					data = logcatService.formatLogcatData(logCtrl.logs, logCtrl.level);
					logCtrl.url = data.csvUrl;
					logCtrl.fileName = data.fileName;
				}
			}
		};

		function changeLevel() {
			logcatService.changeLevel(logCtrl.level, logCtrl.logs);
		};

	}

})();
